package mesweb.mesweb.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farvision.common.api.CommonResult;
import com.farvision.common.exception.ApiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mesweb.mesweb.entity.MesActionset;
import mesweb.mesweb.service.IMesActionsetService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author niufw
 * @since 2021-05-19
 */
@Api(tags="动作组")
@RestController
@RequestMapping("/mes/actionset")
public class MesActionsetController {
    @Autowired
    private IMesActionsetService mesActionsetService;


    @GetMapping("/GetAll")
    @ApiOperation("获取动素类型列表")
    public CommonResult<List<MesActionset>> GetAllActionTherbligsTypes(@ApiParam("动素类型名称") @RequestParam( value = "name",required = false,defaultValue = "") String name) {
        List<MesActionset> data = new ArrayList<>();
        if (StringUtils.isBlank(name) || name.equals("全部")) {
            data = mesActionsetService.list();
        } else {
            QueryWrapper<MesActionset> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("Therbligs_Type_Name", name);
            data = mesActionsetService.list(queryWrapper);
        }
        return CommonResult.success(data);
    }

    /**
     * 分页列表查询
     *
     * @param mesActionset
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value="动作组-分页列表查询", notes="动作组-分页列表查询")
    @GetMapping(value = "/list")
    public CommonResult<?> queryPageList(MesActionset mesActionset,
                                         @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                         @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                         HttpServletRequest req) {

        QueryWrapper<MesActionset> queryWrapper = new QueryWrapper<>();
        Page<MesActionset> page = new Page<MesActionset>(pageNo, pageSize);
        IPage<MesActionset> pageList = mesActionsetService.page(page, queryWrapper);
        return CommonResult.success(pageList);
    }

    /**
     * 添加
     *
     * @param mesActionset
     * @return
     */
    @ApiOperation(value="动作组-添加", notes="动作组-添加")
    @PostMapping(value = "/save")
    public CommonResult<?> add(@RequestBody MesActionset mesActionset) {
        if(mesActionset.getId() == 0 ) {
            QueryWrapper<MesActionset> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("actionset_Name", mesActionset.getActionsetName());
            queryWrapper.or(wrapper -> wrapper.eq("actionset_no", mesActionset.getActionsetNo()));
            MesActionset mesActionset1 = mesActionsetService.getOne(queryWrapper);
            if (mesActionset1 != null) {
                String msg = StrUtil.format("编号{}或者名称{}已经存在！", mesActionset.getActionsetNo(), mesActionset.getActionsetName());
                throw new ApiException(msg);
            }
            mesActionsetService.save(mesActionset);
        }
        else{
            mesActionsetService.updateById(mesActionset);
        }
        return CommonResult.success("添加成功！");
    }

    /**
     * 编辑
     *
     * @param mesActionset
     * @return
     */
    @ApiOperation(value="动作组-编辑", notes="动作组-编辑")
    @PutMapping(value = "/update")
    public CommonResult<?> edit(@RequestBody MesActionset mesActionset) {
        mesActionsetService.updateById(mesActionset);
        return CommonResult.success("编辑成功!");
    }
    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value="动作组-批量删除", notes="动作组-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public CommonResult<?> deleteBatch(@ApiParam("动作组IDs") @RequestBody List<Long>  ids) {
        this.mesActionsetService.removeByIds(ids);
        return CommonResult.success("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value="动作组-通过id查询", notes="动作组-通过id查询")
    @GetMapping(value = "/queryById")
    public CommonResult<?> queryById(@RequestParam(name="id",required=true) String id) {
        MesActionset mesActionset = mesActionsetService.getById(id);
        return CommonResult.success(mesActionset);
    }
}
